from os.path import dirname, abspath
import sys
sys.path.insert(0, dirname(dirname(abspath(__file__))))
import settings
from django.core.management import setup_environ
setup_environ(settings)

from ourcity.reps import models

for r in models.Representative.objects.all():
    print u'Processing rep %s ...' % r
    r_office = r.title.split(',')[0]
    try:
        office = models.Office.objects.get(name=r_office)
        print u'Found office %s' % office
    except models.Office.DoesNotExist:
        print u'Creating office %s ...' % r_office
        office = models.Office(name=r_office)
        office.save()
    r.office = office
    r.simple_save()
